package cn.itcast.nems.oldems.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.itcast.ic.common.bean.Page;
import cn.itcast.nems.base.api.SchoolApi;
import cn.itcast.nems.base.dao.entity.Itcastschool;
import cn.itcast.nems.base.dao.mapper.ItcastschoolMapper;
import cn.itcast.nems.base.dto.SchoolDTO;
import cn.itcast.nems.base.dto.SchoolQueryDTO;
import cn.itcast.nems.common.PageQueryModel;
import cn.itcast.nems.common.account.AccountHolder;
import cn.itcast.nems.common.utils.WrapperUtil;
import cn.itcast.nems.employee.dao.bean.EmployeeGroupBean;
import cn.itcast.nems.manager.util.CustomBeanUtil;
import cn.itcast.nems.oldems.service.SchoolService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 校区相关查询实现类
 * </p>
 *
 * @author zhouwei
 * @since 2023/9/6 6:39 PM
 **/
@Service
@Slf4j
public class SchoolServiceImpl extends ServiceImpl<ItcastschoolMapper, Itcastschool> implements SchoolService, SchoolApi {

    @Override
    public SchoolDTO findById(String id) {
        Itcastschool itcastschool = getBaseMapper().selectOne(
                new WrapperUtil<Itcastschool>()
                        .limitLaseOne("code")
                        .eq(Itcastschool::getCode, id)
                        .eq(Itcastschool::getRunstate, Boolean.TRUE));
        return Objects.nonNull(itcastschool) ?
                CustomBeanUtil.copyProperties(itcastschool, new SchoolDTO()) : null;
    }

    @Override
    public Page<SchoolDTO> findPageList(PageQueryModel<SchoolQueryDTO> queryModel) {
        Assert.notNull(queryModel, "参数为空");
        SchoolQueryDTO queryDTO = Objects.nonNull(queryModel.getCondition()) ? queryModel.getCondition() : new SchoolQueryDTO();
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Itcastschool> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>();
        page.setCurrent(queryModel.getPageNumber());
        page.setSize(queryModel.getPageSize());

        Set<String> schoolCodes = new HashSet<>();
        if (queryDTO.getPermission() != null && queryDTO.getPermission()) {
            if (!AccountHolder.hasEmployeeGroups()) {
                return Page.of(queryModel.getPageNumber(), queryModel.getPageSize(), 0, new ArrayList<>());
            }
            List<EmployeeGroupBean> groupList = AccountHolder.getEmployeeGroups();
            if (groupList != null) {
                for (EmployeeGroupBean employeeGroupBean : groupList) {
                    for (String schoolCode : employeeGroupBean.getSchoolCodeList()) {
                        schoolCodes.add(schoolCode);
                    }
                }
                if (schoolCodes.isEmpty()) {
                    return Page.of(queryModel.getPageNumber(), queryModel.getPageSize(), 0, new ArrayList<>());
                }
            }
        }

        LambdaQueryWrapper<Itcastschool> queryWrapper = new LambdaQueryWrapper<Itcastschool>()
                .eq(StringUtils.hasText(queryDTO.getCode()), Itcastschool::getCode, queryDTO.getCode())
                .in(!schoolCodes.isEmpty(), Itcastschool::getCode, schoolCodes)
                .like(StringUtils.hasText(queryDTO.getName()), Itcastschool::getName, queryDTO.getName())
                .eq(Objects.nonNull(queryDTO.getRunstate()), Itcastschool::getRunstate, queryDTO.getRunstate());
        IPage<Itcastschool> result = getBaseMapper().selectPage(page, queryWrapper);
        List<SchoolDTO> list = null;
        if (Objects.nonNull(result) && CollectionUtil.isNotEmpty(result.getRecords())) {
            list = result.getRecords().stream().map(item -> CustomBeanUtil.copyProperties(item, new SchoolDTO())).collect(Collectors.toList());
        }
        return Page.of(queryModel.getPageNumber(), queryModel.getPageSize(), (int) result.getTotal(), list);
    }

    @Override
    public List<SchoolDTO> getAllSchool() {
        LambdaQueryWrapper<Itcastschool> queryWrapper = new LambdaQueryWrapper<Itcastschool>()
                .eq(Itcastschool::getRunstate, Boolean.TRUE).orderByAsc(Itcastschool::getOrdernumber);
        List<Itcastschool> list = getBaseMapper().selectList(queryWrapper);
        return CollectionUtils.isEmpty(list) ? null :
                list.stream().map(item -> CustomBeanUtil.copyProperties(item, new SchoolDTO())).collect(Collectors.toList());
    }

    @Override
    public List<SchoolDTO> getAllLogicSchool() {
        LambdaQueryWrapper<Itcastschool> queryWrapper = new LambdaQueryWrapper<Itcastschool>()
                .orderByAsc(Itcastschool::getOrdernumber);
        List<Itcastschool> list = getBaseMapper().selectList(queryWrapper);
        return CollectionUtils.isEmpty(list) ? null :
                list.stream().map(item -> CustomBeanUtil.copyProperties(item, new SchoolDTO())).collect(Collectors.toList());
    }


}
